package com.bbx.taxi.client.widget.Dailog;

import java.util.ArrayList;
import java.util.List;

import com.bbx.androidapi.util.SharedPreUtil;
import com.bbx.api.sdk.model.passanger.Return.ActivityContent;
import com.bbx.api.sdk.model.passanger.Return.ActivityContent.Details;
import com.bbx.api.util.SharedPreEncryptUtil;
import com.bbx.taxi.client.R;
import com.bbx.taxi.client.Activity.ActivityDetialActivity;
import com.bbx.taxi.client.Adapter.Page.MyPagerAdapter;
import com.bbx.taxi.client.Bean.Extra.ActivityDetialMsg;
import com.bbx.taxi.client.Util.ImageLoad.ImageLoader;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

/** 
 * @Path com.bbx.taxi.client.widget.Dailog.MyActivityDetailsDialog.java
 * @Author 严旭文
 * @Date 2016-5-24 上午10:18:53
 * @Company 蓝海（福建）信息技术有限公司
 * @Description
 * 活动详情
 */
public class MyActivityDetailsDialog extends Dialog  implements OnPageChangeListener{
	private ImageView imageview;
	private RelativeLayout layout_dailog;
	private LinearLayout dots_top;
	private boolean cancelable = true;
    private Activity context;
    private boolean isFinish=false;
	private ArrayList<View> list_view = new ArrayList<View>();
    public ImageLoader imageLoader; 
    private MyPagerAdapter mMyPagerAdapter;
    private int width;
    private int height;
    private int currentIndex;
	public MyActivityDetailsDialog(Activity context) {
		super(context,R.style.Dialog_bocop);
		this.context=context;
		DisplayMetrics dm=new DisplayMetrics();
		context.getWindowManager().getDefaultDisplay().getMetrics(dm);
		width=(int) (dm.widthPixels*0.8);
		height=(int) (width*1.3);
		init();
	}

	private void init() {
		 imageLoader=new ImageLoader(context.getApplicationContext());
		View contentView = View.inflate(getContext(), R.layout.dailog_activitydetail,
				null);
		setContentView(contentView);
		layout_dailog=(RelativeLayout)findViewById(R.id.layout_dailog);
		dots_top=(LinearLayout)findViewById(R.id.dots_top);
		ViewPager mViewPager=(ViewPager)findViewById(R.id.viewpager);
		mViewPager.setOnPageChangeListener(this);
		RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams) mViewPager.getLayoutParams(); 
		linearParams.width=width;
		linearParams.height=height;
		mViewPager.setLayoutParams(linearParams);
		mMyPagerAdapter=new MyPagerAdapter(list_view);
		mViewPager.setAdapter(mMyPagerAdapter);// 绑定数据
		this.setCancelable(true);
		contentView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (cancelable) {
					dismiss();
				}
			}
		});
	}
	@Override
	public void show() {
		if(MessageDialog.map_dialog!=null){
			int size=MessageDialog.map_dialog.get(context.toString())!=null?MessageDialog.map_dialog.get(context.toString()):0;
			MessageDialog.map_dialog.put(context.toString(), size+1);
			if(MessageDialog.map_dialog.get(context.toString())!=null&&MessageDialog.map_dialog.get(context.toString())<=MessageDialog.max_dialog){
				super.show();
				Animation a = AnimationUtils.loadAnimation(context, R.anim.dialog_in);
				layout_dailog.startAnimation(a);
				layout_dailog.setVisibility(View.VISIBLE);
				isFinish=false;
			}else{
				MessageDialog.map_dialog.put(context.toString(), MessageDialog.map_dialog.get(context.toString())-1);
			}
			}
	}

	@SuppressLint("NewApi")
	@Override
	public void dismiss() {
		if(isFinish)
		super.dismiss();
		int size=MessageDialog.map_dialog.get(context.toString())!=null?MessageDialog.map_dialog.get(context.toString()):0;
		if(size>0){
			MessageDialog.map_dialog.put(context.toString(), size-1);
		}
		Animation a = AnimationUtils.loadAnimation(context, R.anim.dialog_out);
		layout_dailog.startAnimation(a);
		a.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation anim) {}
			
			@Override
			public void onAnimationRepeat(Animation anim) {}	
			@Override
			public void onAnimationEnd(Animation anim) {
				isFinish=true;
				dismiss();
			}
		});

	}

	@Override
	public void setCancelable(boolean flag) {
		cancelable = flag;
		super.setCancelable(flag);
	}
	public void setContent(ActivityContent mActivityContent){
		list_view.clear();
		if(mActivityContent!=null&&!mActivityContent.getList().isEmpty()){
			long interval=0;
			final List<Details> list = mActivityContent.getList();
			for(int i=0;i<list.size();i++){
				final int postion =i;
				View v = View.inflate(getContext(), R.layout.imageview,null);
				ImageView imageview=(ImageView)v.findViewById(R.id.pic_hb);
				imageview.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if(list.get(postion).gatEway()!=null&&!list.get(postion).gatEway().equals("")){
						dismiss();
						context.startActivity(new Intent(context,ActivityDetialActivity.class)
						.putExtra(ActivityDetialMsg.URL, list.get(postion).gatEway())
						.putExtra(ActivityDetialMsg.NAME, list.get(postion).name));
						}
					}
				});
				imageLoader.DisplayImage(list.get(i).getPoster(), imageview,true);
				list_view.add(v);
				if(list.get(i).getTime()<interval||interval==0){
					interval=list.get(i).getTime();
				}
				
			}
			int dot_margin_lr = (int) context.getResources().getDimension(
					R.dimen.chat_dot_margin_lr);
			int dot_wh_big = (int)context. getResources().getDimension(
					R.dimen.chat_dot_wh_big);
			for (int i = 0; i < list_view.size(); i++) {
				android.widget.LinearLayout.LayoutParams lp = new android.widget.LinearLayout.LayoutParams(
						dot_wh_big, dot_wh_big);
				lp.leftMargin = dot_margin_lr;
				lp.rightMargin = dot_margin_lr;
				dots_top.addView(dotsItem(i), lp);
			}
			currentIndex = 0;
			dots_top.getChildAt(currentIndex).setSelected(true);
			SharedPreUtil.putLongValue(context,
					SharedPreEncryptUtil.activity_interval,interval);
		}
		mMyPagerAdapter.notifyDataSetChanged();
	}
	/**
	 * ，底部小圆点
	 * 
	 * @param position
	 * @return
	 */
	private RelativeLayout dotsItem(int position) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.dot_image, null);
		RelativeLayout iv = (RelativeLayout) layout.findViewById(R.id.face_dot);
		iv.setId(position);
		return iv;
	}
	private void setCurrentDot(int position) {		
		for (int i = 0; i < dots_top.getChildCount(); i++) {
			dots_top.getChildAt(i).setSelected(false);
		}
		dots_top.getChildAt(position).setSelected(true);

		currentIndex = position;
	}
	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageSelected(int arg0) {
		// 设置底部小点选中状态
		setCurrentDot(arg0);
	}
}
